<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="shop-info-box">
					<view class="info-item flexWrapNo">
						<view class="item-title">任务名称:</view>
						<input placeholder="请输入" placeholder-class="placeholderclass" v-model="task_name"/>
					</view>
					<view class="info-item flexWrapNo">
						<view class="flexWrapNo" style="flex: 1;">
							<view class="item-title">任务周期:</view>
							<picker @change="cycChange" :value="index" :range="cycleArray" range-key="name">
								<view class="select-input">{{cycleArray[cycleIndex].name}}</view>
							</picker>
						</view>
						<text class="icon-more"></text>
					</view>
					<view class="info-item flexWrapNo" v-if="cycleIndex==2">
						<view class="item-title">任务周期:</view>
						<view class="flexWrap">
							<picker mode="date" :data-index='0' :value="starTime" @change="bindDateChange" :start="date">
								<view class="select-input" v-if="starTime">{{starTime}}</view>
								<view class="select-input" v-else>请选择开始时间</view>
							</picker>
							<text class="small">至</text>
							<picker mode="date" :data-index='1' :value="date" @change="bindDateChange" :start="starTime">
								<view class="select-input" v-if="endTime">{{endTime}}</view>
								<view class="select-input" v-else>请选择结束时间</view>
							</picker>
						</view>
					</view>
					<view class="remaks-box">
						备注：周任务\月任务会在每周\每月第一天对任务记录清零，重新开始计算。只要优惠券发放完，自动结束。
					</view>
					<view class="info-item flexWrapNo">
						<view class="flexWrapNo" style="flex: 1;">
							<view class="item-title">任务类型:</view>
							<picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
								<view class="select-input">{{array[index].name}}</view>
							</picker>
						</view>
						<text class="icon-more"></text>
					</view>
					<view class="info-item flexWrapNo">
						<view class="item-title">任务目标:</view>
						<input placeholder="请输入目标排名/小时" placeholder-class="placeholderclass" v-model="task_target" type="number"/>
					</view>
					<view class="remaks-box">
						<block v-if="index==0">备注：如任务目标3小时，则在任务周期内，消费超过3小时可获取奖励，不限支付方式。</block>
						<block v-else>
							备注：如目标排行前3，则在任务周期内，消费时长前3小时用户可获取奖励，不限支付方式。
						</block>
					</view>
					<view class="info-item flexWrapNo" @tap="goPage('/pages/manage/couponList?isSelect=1')">
						<view class="item-title">奖励优惠券:</view>
						<view class="flexWrapNo" style="flex: 1;">
							<input placeholder="选择优惠券" placeholder-class="placeholderclass" disabled v-model="coupon_name"/>
							<text class="icon-more"></text>
						</view>
					</view>
					<view class="info-item flexWrapNo">
						<view class="item-title">奖励数量:</view>
						<input placeholder="请输入优惠券数量" placeholder-class="placeholderclass" v-model="coupon_num" type="number"/>
					</view>
					<view class="remaks-box">
						备注：每个达到任务目标的用户奖励的优惠券数量。请保证优惠券余量充足，优惠券数量不足，会自动结束任务。
					</view>
					<view class="info-item flexWrapNo">
						<view class="item-title">任务说明:</view>
						<textarea placeholder="请输入任务说明" placeholder-class="placeholderclass" v-model="task_desc"></textarea>
					</view>
				</view>
				<view class="determine-btn" @tap="taskEdit">提交保存</view>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			const starDate = this.getDate('start')
			return {
				pageOpc: 0,
				array: [
					{
						name:'累计消费时长',
						id:1
					},
					// {
					// 	name:'消费排名',
					// 	id:2
					// }
				],
				index: 0,
				starTime: starDate,
				endTime: currentDate,
				date: currentDate,
				couponId:"",
				task_name:"",
				task_type:1,
				task_target:"",
				task_desc:"",
				coupon_num:"",
				coupon_name:"",
				taskId:"",
				cycleArray:[
					{
						name:'周任务',
						id:1
					},
					{
						name:'月任务',
						id:2
					},
					{
						name:'自定义周期',
						id:3
					}
				],
				cycleIndex:0,
				cycle_type:1
			}
		},
		onLoad(ops) {
			this.taskId=ops.taskId||""
			if(this.taskId) this.getTaskInfo()
		},
		onReady() {
			setTimeout(() => {
				this.pageOpc = 1
			}, 100)
		},
		methods: {
			// 获取任务信息
			getTaskInfo(){
				this.$request.post(this.$api.taskInfo, {
					store_id:uni.getStorageSync('store_id'),
					task_id:this.taskId
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						this.task_name=data.task_name
						this.cycle_type=data.cycle_type
						this.cycleIndex=Number(data.cycle_type-1)
						this.starTime=data.start_date
						this.endTime=data.end_date
						this.task_type=data.task_type
						this.task_target=data.task_target
						this.task_desc=data.task_desc
						this.couponId=data.coupon_id
						this.coupon_num=data.coupon_num
						this.getCouponInfo();
					}
				})
			},
			// 获取优惠券信息
			getCouponInfo(){
				this.$request.post(this.$api.couponInfo, {
					store_id:uni.getStorageSync('store_id'),
					coupon_id:this.couponId
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						this.coupon_name=data.title
					}
				})
			},
			// 新建、修改任务
			taskEdit(){
				if(!this.task_name){
					this.$tools.showToast('请填写任务说明');
					return;
				}else if(this.cycle_type==3&&!this.starTime){
					this.$tools.showToast('请选择开始时间');
					return;
				}else if(this.cycle_type==3&&!this.endTime){
					this.$tools.showToast('请选择结束时间');
					return;
				}else if(!this.task_target){
					this.$tools.showToast('请填写任务目标');
					return;
				}else if(!this.couponId){
					this.$tools.showToast('请选择优惠券');
					return;
				}else if(!this.coupon_num){
					this.$tools.showToast('请填写优惠券数量');
					return;
				}
				let requestUrl=""
				if(this.taskId){
					requestUrl=this.$api.taskEdit
				}else{
					requestUrl=this.$api.taskAdd
				}
				this.$request.post(requestUrl, {
					store_id:uni.getStorageSync('store_id'),
					task_name:this.task_name,
					cycle_type:this.cycle_type,
					start_date:this.cycle_type==3?this.starTime:"",
					end_date:this.cycle_type==3?this.endTime:"",
					task_type:this.task_type,
					task_target:this.task_target,
					task_desc:this.task_desc,
					coupon_id:this.couponId,
					coupon_num:this.coupon_num,
					id:this.taskId
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						this.$tools.showToast('操作成功');
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
					}
				})
			},
			// 获取时间
			getDate(type) {
				const date = new Date();
				date.setDate(date.getDate());
				// if (type === 'start') {
				// 	date.setDate(date.getDate());
				// }
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			bindPickerChange: function(e) {
				this.index = e.detail.value
				this.task_type=this.array[this.index].id
			},
			cycChange(e){
				this.cycleIndex = e.detail.value
				this.cycle_type=this.cycleArray[this.cycleIndex].id
			},
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.starTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
			},
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 0 36upx;

		.shop-info-box {
			.info-item {
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 40upx;
				color: #FFFFFF;
				padding: 30upx 0;
				border-bottom: 1upx solid #36355E;
				picker{
					flex: 1;
				}
				.item-title{
					margin-right: 32upx;
					flex: none;
				}
				.small{
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					margin:0 40upx;
				}
				.select-input{
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
					flex: 1;
				}
				input {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
					flex: 1;
				}

				.icon-more {
					border-color: #FFFFFF;
				}

				.placeholderclass {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
				}
				textarea{
					background: #1D1C5C;
					border-radius: 16upx;
					padding: 30upx;
					height: 140upx;
				}
			}
			.remaks-box{
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #82819B;
				line-height: 40rpx;
				padding-left: 158upx;
				margin-top: 12upx;
			}
		}
		.determine-btn{
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			width: 322rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #26BCFD;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 78upx auto;
		}
	}
</style>
